<?php
// +----------------------------------------------------------------------
// |个人资料
// +----------------------------------------------------------------------
// |Data:2016-06-27 上午11:50
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Wap\Controller;
use Common\Controller\CommonController;

class InfoController extends CommonController{
    private $_model;
    protected function _initialize(){
        parent::_initialize();
        $this->_model = D('Wap/Info');
    }

    public function index(){
        $detail = $this->_model->getUserInfo($this->uid);
        $detail['pic'] = showImage($detail['face'],'face',$this->uid)[0];
        $this->assign('detail',$detail);
        $this->display();
    }

    public function info(){
        $data = I('get.');
        $detail = $this->_model->getUserInfo($this->uid);
        if($detail['area']){
            $detail['area'] = explode(',',$detail['area']);
        }
        $address = D('Wap/Address')->getUseAddress($this->uid);
        $this->assign('address',$address);
        $this->assign('detail',$detail);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 我的订单(记录)
     */
    public function order(){
        $this->_order();
        $this->display();
    }

    /**
     * 我的订单(待评价)
     */
    public function order_wait(){
        $this->_order(2);
        $this->display('order_wait');
    }

    /**
     * @param int $type //1下单记录  2待评价
     */
    private function _order($type = 1){
        $map = array();
        $map['uid'] = $this->uid;
        if($type == 2){
            $map['status'] = 5;
            $map['is_comment'] = array('neq',1);
        }
        $list = D('Wap/Order')->getUserOrderList($map,10);
        //麻辣味小龙虾（小份量）1份
        foreach($list['data'] as &$val){
            $titleAttr = array();
            $extendList = D('Wap/Order')->getOrderExtendList($val['id']);
            foreach($extendList as $va){
                $detail = D('Admin/Product')->detail($va['product_id']);
                if($va['attr']){
                    $attr = unserialize($detail['attr']);
                    foreach($attr as $v){
                        if($v['key'] == $va['attr']){
                            $str_title = $detail['title'].'('.$v['name'].')'.$va['num'].$detail['unit'];
                            array_push($titleAttr,$str_title);
                        }
                    }
                }else{
                    $str_title = $detail['title'].$va['num'].$detail['unit'];
                    array_push($titleAttr,$str_title);
                }
            }
            $shopDetail = D('Common/Shop')->detail($val['shop_id']);
            $val['shopTitle'] = $shopDetail['title'];
            $val['title'] = implode(',',$titleAttr);
        }

        $this->assign('type',$type);
        $this->assign($list);
    }

    /**
     * 订单详情
     * @return bool
     */
    public function detail(){
        $order_id = I('get.id');
        if(empty($order_id)) return false;

        $orderDetail = D('Wap/Order')->detail($order_id);
        if(empty($orderDetail)) return false;
        $address = D('Wap/Address')->detail($orderDetail['address_id']);
        $extendList = D('Wap/Order')->getOrderExtendList($order_id);
        $data = array();
        foreach($extendList as $val){
            $detail = D('Admin/Product')->detail($val['product_id']);
            if($val['attr']) {
                $attr = unserialize($detail['attr']);
                foreach($attr as $v){
                    if($val['attr'] == $v['key']){
                        $temp = array(
                            'title'=>$detail['title'],
                            'name'=>$v['name'],
                            'price'=>$v['price'],
                            'num'=>$val['num'],
                        );
                        array_push($data,$temp);
                    }
                }
            }else{
                $temp = array(
                    'title'=>$detail['title'],
                    'num'=>$val['num'],
                    'price'=>$val['unitprice'],
                );
                array_push($data,$temp);
            }
        }
        $shopDetail = D('Common/Shop')->detail($orderDetail['shop_id']);
        $orderDetail['shopTitle'] = $shopDetail['title'];
        $orderDetail['shopTel'] = $shopDetail['tel'];
        $this->assign('address',$address);
        $this->assign('detail',$orderDetail);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 确认完成
     */
    public function complete(){
        if(IS_POST){
            $id = I('post.id');
            empty($id) && $this->returnData('请选择操作项!');
            $data = array(
                'sign_time'=>time(),
                'status'=>5,
                'id'=>$id
            );
            $result = D('Wap/Order')->update($data);
            if($result){
                $this->returnData('确定成功!',1);
            }else{
                $this->returnData('确定失败!');
            }
        }else{
            $this->returnData('请求方式错误!');
        }
    }

    /**
     * 评论
     */
    public function comment(){
        if(IS_POST){
            $order_id = I('post.id');
            $score = I('post.score',0);
            $content = I('post.content');

            empty($order_id) && $this->returnData('请选择评论订单!');
            empty($score) && $this->returnData('请给予评论打分!');
            empty($content) && $this->returnData('请输入评论内容!');
            if(abslength($content) > 100) $this->returnData('评论内容长度1~100个字符!');
            $detail = D('Wap/Order')->detail($order_id);
            empty($detail) && $this->returnData('该订单不存在!');
            if($detail['status'] != 5) $this->returnData('该订单状态错误!');

            $user = D('Wap/Info')->detail($this->uid);
            $data = array(
                'order_id'=>$order_id,
                'uid'=>$this->uid,
                'nickname'=>$user['nickname'],
                'shop_id'=>$detail['shop_id'],
                'content'=>$content,
                'add_time'=>time(),
                'replay_content'=>'',
                'score'=>$score
            );
            $result = D('Wap/Comment')->doComment($order_id,$data);
            if($result){
                $this->returnData('评论成功!',1);
            }else{
                $this->returnData('评论失败!',1);
            }
        }else{
            $order_id = I('get.id');
            if(empty($order_id)) return false;
            $detail = D('Wap/Order')->detail($order_id);
            if(empty($detail)) return false;
            if($detail['status'] != 5) return false;

            $shopDetail = D('Common/Shop')->detail($detail['shop_id']);
            $this->assign('title',$shopDetail['title']);
            $this->assign('order_id',$order_id);
            $this->display();
        }
    }
}
?>